﻿@inherits MudComponentBase

@inject IStringLocalizer<Roles> L

<MudDrawer Open="@Open"
           Width="400px"
           Height="100vh"
           OpenChanged="@(s => OnOpenChanged.InvokeAsync(s))"
           Anchor="Anchor.End" Elevation="1" Variant="@DrawerVariant.Temporary">
    <MudDrawerHeader>
        <MudText Typo="Typo.h6">Set Permissions</MudText>
    </MudDrawerHeader>
    <MudStack AlignItems="AlignItems.Stretch" Class="px-5">
        <MudTextField @bind-Value="Keyword" Label="Search"
                      Margin="Margin.Dense"
                      Variant="Variant.Text"
                      Adornment="Adornment.End"
                      AdornmentIcon="@Icons.Material.Filled.Search"
                      AdornmentColor="Color.Secondary" />
    </MudStack>
    @if (Permissions != null && Permissions.Any())
    {
        @foreach (var group in Permissions.Where(x => x.Group!.Contains(Keyword, StringComparison.InvariantCultureIgnoreCase) || x.Description!.Contains(Keyword, StringComparison.InvariantCultureIgnoreCase)).Select(x => x.Group).Distinct().ToList())
        {
            <MudCard class="ma-4">
                <MudCardHeader>
                    <CardHeaderContent>
                        <MudStack Spacing="2">
                            <MudText Typo="Typo.body1">
                                @L[group]
                            </MudText>
                            <MudText Typo="Typo.body2">
                                @(L[Permissions.First(x => x.Group == group).Description])
                            </MudText>
                        </MudStack>
                    </CardHeaderContent>
                    <CardHeaderActions>
                        <MudTooltip Text="@L["Assign or Unassign all"]">
                            <MudIconButton Disabled="@Waiting" Icon="@Icons.Material.Filled.AutoFixHigh" Color="Color.Default" OnClick="@(() => OnAssignAll(group))" />
                        </MudTooltip>
                    </CardHeaderActions>
                </MudCardHeader>
                <MudCardContent>
                    @for (var i = 0; i < Permissions.Count; i++)
                    {
                        var x = i;
                        if (Permissions[x].Group == group)
                        {
                            <MudTooltip Delay="400" Text="@L[Permissions[x].HelpText??Permissions[x].Name]">
                                <MudCheckBox Dense="true"
                                             T="bool"
                                             Size="Size.Small"
                                             Value="@Permissions[x].Assigned"
                                             Label="@L[Permissions[x].Name]"
                                             ValueChanged="@(s => OnAssignChanged.InvokeAsync(Permissions[x]))"
                                             Color="Color.Primary">
                                </MudCheckBox>
                            </MudTooltip>
                        }
                    }
                </MudCardContent>

            </MudCard>
        }
    }
    else
    {
        <MudProgressLinear Color="Color.Primary" Indeterminate="true" Class="my-7" />
    }
</MudDrawer>

@code {
    [EditorRequired][Parameter] public IList<PermissionModel>? Permissions { get; set; }

    [EditorRequired][Parameter] public bool Open { get; set; } = default!;

    [EditorRequired][Parameter] public EventCallback<PermissionModel> OnAssignChanged { get; set; }

    [EditorRequired][Parameter] public EventCallback<List<PermissionModel>> OnAssignAllChanged { get; set; }

    [EditorRequired][Parameter] public EventCallback<bool> OnOpenChanged { get; set; }

    [Parameter] public bool Waiting { get; set; }

    private string Keyword { get; set; } = string.Empty;

    private async Task OnAssignAll(string? groupName)
    {
        var list = new List<PermissionModel>();
        foreach (var t in Permissions!.Where(t => t.Group == groupName))
        {
            t.Assigned = !t.Assigned;
            list.Add(t);
        }

        await OnAssignAllChanged.InvokeAsync(list);
    }

}